﻿#region
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Y2M.Components;

#endregion

namespace Y2M.Controls
{
    public partial class ClipControl : UserControl
    {
        private ClipControl()
        {
            InitializeComponent();
        }

        public ClipControl(Clip clip) : this()
        {
            Clip = clip;

            lblTitle.Text = Clip.Title;
            lblDescription.Text = Clip.Description;
        }

        public Clip Clip { get; private set; }

        public void LoadThumbnail()
        {
            ptbThumbnail.Image = Image.FromStream(new MemoryStream(Clip.Thumbnail));
        }

        private void hleDownloadAudio_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            RaiseCommandExecuted(ClipCommand.DownloadAudio, Clip);
        }

        private void hleDownloadVideo_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            RaiseCommandExecuted(ClipCommand.DownloadVideo, Clip);
        }

        private void hleGotoUtube_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            RaiseCommandExecuted(ClipCommand.GotoUtubePage, Clip);
        }

        private void hleShowRelated_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            RaiseCommandExecuted(ClipCommand.ShowRelated, Clip);
        }

        private void hleShowByUploader_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            RaiseCommandExecuted(ClipCommand.ShowByUploader, Clip);
        }

        private void RaiseCommandExecuted(ClipCommand clipCommand, Clip clip)
        {
            Cursor = Cursors.WaitCursor;

            OnCommandExecuted(clipCommand, clip);

            Cursor = Cursors.Arrow;
        }

        #region CommandExecuted event things for C# 3.0
        public event EventHandler<CommandExecutedEventArgs> CommandExecuted;

        protected virtual void OnCommandExecuted(CommandExecutedEventArgs e)
        {
            if (CommandExecuted != null)
                CommandExecuted(this, e);
        }

        protected virtual void OnCommandExecuted(ClipCommand command, Clip clip)
        {
            OnCommandExecuted(new CommandExecutedEventArgs(command, clip));
        }

        protected virtual CommandExecutedEventArgs OnCommandExecutedWithReturn(ClipCommand command, Clip clip)
        {
            CommandExecutedEventArgs args = new CommandExecutedEventArgs(command, clip);
            OnCommandExecuted(args);

            return args;
        }

        public class CommandExecutedEventArgs : EventArgs
        {
            public ClipCommand Command { get; set; }
            public Clip Clip { get; set; }

            public CommandExecutedEventArgs()
            {
            }

            public CommandExecutedEventArgs(ClipCommand command, Clip clip)
            {
                Command = command;
                Clip = clip;
            }
        }
        #endregion
    }
}